<template>
  <div>
    <el-row>
        <el-col :span="18" style="text-align: left;">
            <el-form :inline="true" :model="queryApplicationForm" class="demo-form-inline">
                <el-form-item label="仓库">
                    <el-select v-model="queryApplicationForm.warehouseId" placeholder="请选择仓库">
                        <el-option
                        v-for="item in warehouses"
                        :key="item.warehouseId"
                        :label="item.warehouseName"
                        :value="item.warehouseId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <!-- <el-form-item label="审核状态">
                    与审核筛选有冲突，取消功能
                    <el-input v-model="queryApplicationForm.checkType" placeholder="请选择审核状态"></el-input>
                </el-form-item> -->
                <el-form-item label="部门名称" v-if="user.role.indexOf('5') != -1">
                    <el-select v-model="queryApplicationForm.deptNo" placeholder="请选择部门">
                        <el-option
                        v-for="item in dept"
                        :key="item.deptno"
                        :label="item.dname"
                        :value="item.deptno">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="申请人">
                    <el-input v-model="queryApplicationForm.applicationEName" placeholder="请输入申请人姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="query">查询</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right;">
            <el-button-group>
                <!-- <el-button type="primary" @click="addApplication">新增</el-button>
                <el-button type="primary" @click="delApplication">删除</el-button> -->
                <el-button type="primary" @click="viewProcess">流程图</el-button>
            </el-button-group>
        </el-col>
    </el-row>

    <el-row>
        <el-col :span="22" :offset="1">
            <el-table
                ref="multipleTable"
                height="600px"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                stripe
                border
                highlight-current-row
                @selection-change="handleSelectionChange">
                <el-table-column
                type="selection"
                align="center"
                width="40">
                </el-table-column>
                <el-table-column
                prop="applicationId"
                label="申请编号"
                align="center"
                width="80">
                </el-table-column>
                <el-table-column
                prop="applicationName"
                label="申请类型"
                align="center"
                width="90">
                </el-table-column>
                <el-table-column
                prop="applicationEName"
                label="申请人"
                align="center"
                width="100">
                </el-table-column>
                <el-table-column
                prop="dname"
                label="申请人部门"
                align="center"
                width="100">
                </el-table-column>
                <el-table-column
                label="借用仓库"
                align="center"
                width="120">
                <template slot-scope="scope">{{ warehouses[scope.row.warehouseId - 1].warehouseName }}</template>
                </el-table-column>
                <el-table-column
                prop="comment"
                label="申请原因"
                align="left"
                width="100">
                </el-table-column>
                <el-table-column
                prop="borrowTypeDESC"
                label="借用类型"
                align="center"
                width="100">
                </el-table-column>
                
                <el-table-column
                prop="applicationTime"
                label="申请时间"
                align="center"
                width="120">
                </el-table-column>
                <!-- <el-table-column
                prop="auditEName"
                label="审核人"
                width="120">
                </el-table-column> -->
                <el-table-column 
                prop="checkTime"
                label="审核时间"
                align="center"
                width="120">
                </el-table-column>
                <el-table-column
                prop="checkTypeDESC"
                label="审核状态"
                align="center"
                width="120">
                </el-table-column>
                
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button
                        size="mini"
                        @click="rowEditApplication(scope.$index, scope.row)" v-if="checkableCheckType.indexOf(scope.row.checkType) != -1">审核申请</el-button>
                        <el-button
                        size="mini"
                        @click="viewApplication(scope.$index, scope.row)" v-if="checkableCheckType.indexOf(scope.row.checkType) == -1">查看申请</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 15, 20]"
            :page-size="100"
            layout=" prev, pager, next, total, sizes,jumper"
            :total="total">
            </el-pagination>
        </el-col>
    </el-row>
    <BorrowProcess :isShow="dialogVisible" :appplicationCheckType="proseccCheckType" @closeDialog="closeDialog"></BorrowProcess>
  </div>
</template>

<script>
import BorrowProcess from '@/components/BorrowProcess.vue';
export default {
    name: 'CheckBApplicationManager',
    components: { BorrowProcess },
    data(){
        return{
            queryApplicationForm: {
                warehouseId:"",
                checkType:0,
                deptNo:'',
                applicationEmpNo:0,
                applicationEName:'',
                applicationName:"借用申请",
                filter:true,
                current:1,
                size:10
            },
            checkableCheckType:'',
            warehouses:[],
            tableData: [],
            total:0,
            user:{
                role:''
            },
            checkData:[],
            dept:[],
            readProcessId:0,
            proseccCheckType:0,
            dialogVisible:false,
            
        }
    },
    methods:{
        query(){
            this.judgeCheckType()
            // console.log(this.queryApplicationForm.deptNo)
            // console.log('1.'+this.queryApplicationForm.checkType)
            if (localStorage.getItem("deptno").indexOf(this.queryApplicationForm.deptNo) == -1){
                this.queryApplicationForm.checkType = 3
                console.log('2.'+this.queryApplicationForm.checkType)
            }
            this.$http.post("/lc/getApplicationWithQueryApplicationDTO",this.queryApplicationForm).then(res => {
                this.tableData = res.data.data;
                this.total = res.data.total;
            }).catch(err => {
              console.error("Error querying applications:", err);
              this.$message.error("查询申请单失败: " + err.message);
            })
        },
        getWarehouse(){
            if (localStorage.getItem('roleid').indexOf('5') != -1 && localStorage.getItem('roleid').indexOf('3') == -1){
                this.$http.get("/lc/queryWarehouseByEmpNo?empNo="+localStorage.getItem('empno')).then(res => {
                    this.warehouses = res.data;
                    this.queryApplicationForm.warehouseId = this.warehouses[0].warehouseId
                    this.query()
                }).catch(err => {
                  console.error("Error getting warehouses:", err);
                  this.$message.error("获取仓库信息失败: " + err.message);
                })
            } else {
                this.$http.get("/lc/queryWarehouse").then(res => {
                    this.warehouses = res.data;
                    this.queryApplicationForm.warehouseId = this.warehouses[0].warehouseId
                    this.query()
                }).catch(err => {
                  console.error("Error getting warehouses:", err);
                  this.$message.error("获取仓库信息失败: " + err.message);
                })
            }
            
        },
        getDept(){
            this.$http.get("/lc/getDepts").then(res => {
                this.dept = res.data;
            }).catch(err => {
              console.error("Error getting departments:", err);
              this.$message.error("获取部门信息失败: " + err.message);
            })
        },
        viewProcess(){
            if (this.checkData.length != 1){
                this.$message.error("请选择一条数据")
                return
            }
            this.proseccCheckType = this.checkData[0].checkType
            this.dialogVisible = true
        },
        closeDialog(){
            this.dialogVisible = false
            this.proseccCheckType = 0
        },
        handleSelectionChange(val){
            this.checkData = val
        },
        handleCurrentChange(){
            this.queryApplicationForm.current = val;
            this.query()
        },
        handleSizeChange(){
            this.queryApplicationForm.size = val;
            this.query()
        },
        rowEditApplication(index,row){ 
            // console.log(row)
            this.$http.get("/lc/getApplicationGoods?applicationId="+row.applicationId).then(res => {
                // console.log(res.data)
                // console.log(row)
                for (let goods of res.data){
                    goods.warehouseName = this.warehouses[row.warehouseId-1].warehouseName
                }
                row.dName = row.dname
                this.$router.push({
                    path:"/checkBApplcation",
                    query:{
                        applicationInfo:row,
                        applicationGoods:res.data,
                        readonly:false
                    }
                })
            }).catch(err => {
              console.error("Error getting application goods:", err);
              this.$message.error("获取申请物资信息失败: " + err.message);
            })
        },
        viewApplication(index,row){
            // console.log(row)
            this.$http.get("/lc/getApplicationGoods?applicationId="+row.applicationId).then(res => {
                // console.log(res.data)
                // console.log(row)
                for (let goods of res.data){
                    goods.warehouseName = this.warehouses[row.warehouseId-1].warehouseName
                }
                row.dName = row.dname
                this.$router.push({
                    path:"/checkBApplcation",
                    query:{
                        applicationInfo:row,
                        applicationGoods:res.data,
                        readonly:true
                    }
                })
            }).catch(err => {
              console.error("Error getting application goods:", err);
              this.$message.error("获取申请物资信息失败: " + err.message);
            })
        },
        
        judgeCheckType(){
            if (localStorage.getItem('roleid').indexOf('5') != -1){
                this.queryApplicationForm.checkType = 3
            }
            if (localStorage.getItem('roleid').indexOf('3') != -1){
                this.queryApplicationForm.checkType = 2
            }
        }
    },
    mounted(){
        
        this.getDept()

        let cType = []
        if (localStorage.getItem('roleid').indexOf('5') != -1){
            cType.push(3)
        } 
        if (localStorage.getItem('roleid').indexOf('3') != -1){
            cType.push(2)
        }
        this.checkableCheckType = cType + ''

        if (this.$route.query.msg != null){
            this.$message.success(this.$route.query.msg)
        }
        
        this.user.role = localStorage.getItem("roleid")
        if (localStorage.getItem("roleid").indexOf("3") != -1){
            this.queryApplicationForm.deptNo = parseInt(localStorage.getItem("deptno"))
        }
        this.getWarehouse()
        
    }

}
</script>

<style>

</style>